package com.yiqi.zhiyuan.feature.certification.view;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.DensityUtil;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.vo.CarColorVO;
import com.yiqi.zhiyuan.feature.certification.view.adapter.ColorAdapter;
import com.yiqi.zhiyuan.framework.DataServer;
import com.yiqi.zhiyuan.framework.api.CarApi;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class SelectColorActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_color)
    RecyclerView mRvColor;
    private ColorAdapter mAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_select_color;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        initToolBar(mToolbar,"车辆颜色",true,true);
    }

    @Override
    protected void initData() {
        initAdapter();

        AM.api().createApiService(CarApi.class)
                .getAllColors()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<CarColorVO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(List<CarColorVO> listResult) {
                        dismissLoading();
                        mAdapter.setNewData(listResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                        showMsg(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }

    private void initAdapter() {
        mRvColor.setLayoutManager(new LinearLayoutManager(this));
        mRvColor.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(DensityUtil.dp2px(1)));
        mAdapter = new ColorAdapter();
        mAdapter.bindToRecyclerView(mRvColor);
        mAdapter.setOnItemClickListener(this);
    }
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent();
        intent.putExtra("color", mAdapter.getItem(position).getColourName());
        setResult(-1, intent);
        finish(); //结束当前的activity的生命周期
    }
}
